package com.huang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huang.mapper.MenuMapper;
import com.huang.mapper.RoleMapper;
import com.huang.pojo.Menu;
import com.huang.pojo.MenuExample;
import com.huang.pojo.Role;
import com.huang.pojo.RoleExample;
import com.huang.utils.ToListInt;
import com.huang.vo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class RoleService {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private RoleMapper roleMapper;

    /**
     * 查询所有 角色
     * @return
     */
    public List<Role> getAll(){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        // 查找未删除的
        criteria.andIsDelEqualTo(0);
        return roleMapper.selectByExample(roleExample);
    }

    /**
     * 分页+条件模糊查询
     * @param r_name
     * @param pageIndex
     * @param pageSize
     * @return 返回带有分页信息的对象
     */
    public PageResult list(String r_name, Integer pageIndex, Integer pageSize){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (r_name != null && !"".equals(r_name)){
            criteria.andR_nameLike("%"+r_name+"%");
        }
        // 查找未删除的
        criteria.andIsDelEqualTo(0);
        PageHelper.startPage(pageIndex,pageSize);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> info = new PageInfo<>(roles);
        // 封装数据
        PageResult pageResult = new PageResult(roles,
                (int) info.getTotal(),
                info.getPageNum(),
                info.getPages(),
                info.getPrePage(),
                info.getNextPage(),
                1,
                info.getPages(),
                info.getNavigatepageNums());
        return pageResult;
    }


    /**
     * 通过roleId查找相连的资源list
     * @param id
     * @return
     */
    public Role findById(Integer id){
        // 先查询role
        // 查询未被删除的
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andR_idEqualTo(id);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        Role role = roles.get(0);
        // 通过roleId 查询 menu菜单
        List<Menu> menus = roleMapper.selectMenusByRoleId(id);
        role.setMenuList(menus);
        return role;
    }

    /**
     * 通过主键更新信息
     * @param role
     * @return
     */
    public int update(Role role){
        // 更新 role表
        roleMapper.updateByPrimaryKeySelective(role);
        // 删除 role_menu表 + 新增role_menu表
        roleMapper.deleteRoleAndMenuByRoleId(role.getR_id());
        int i = roleMapper.insertToRoleAndMenu(role.getMenu(), role.getR_id());
        return i;
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        Role role = new Role();
        role.setIsDel(1);
        role.setR_id(id);
        int i = roleMapper.updateByPrimaryKeySelective(role);
        return i;
    }

    /**
     * 逻辑批量删除
     * @param ids 字符串数组
     * @return
     */
    public int delete(String[] ids){
        // 将 String数组转换为 list<Integer>
        List<Integer> list = ToListInt.StringToList(ids);
        int i = roleMapper.deleteByBatch(list);
        return i;
    }

    /**
     * 根据name查找是否有该资源
     * @param name
     * @return 对象信息
     */
    public Role isExistName(String name){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andR_nameEqualTo(name);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return roles.size()==0?null:roles.get(0);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    public int insert(Role role){
        role.setIsDel(0);
        int insert = roleMapper.insert(role);
        System.out.println(role.getR_id());
        // 添加 role_menu表
        int i = roleMapper.insertToRoleAndMenu(role.getMenu(), role.getR_id());
        return i;
    }
}
